<template>
  <list-card>
    <el-form slot="search" ref="queryForm" inline :model="queryParams" label-suffix="：" size="small">
      <el-form-item label="配置编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入配置编码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="配置名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入配置名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否默认" prop="defaulted">
        <el-switch v-model="queryParams.defaulted" active-text="是" inactive-text="否"/>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div slot="operate">
      <el-button v-has-permi="['system:oss:add']" icon="el-icon-plus" plain size="mini" type="primary"
                 @click="handleAdd">新增
      </el-button>
      <el-button v-has-permi="['system:oss:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <el-table slot="content" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column v-if="false" align="center" label="主建" prop="ossConfigId"/>
      <el-table-column align="center" label="配置key" prop="configKey"/>
      <el-table-column align="center" label="访问站点" prop="endpoint" width="200"/>
      <el-table-column align="center" label="自定义域名" prop="domain" width="200"/>
      <el-table-column align="center" label="桶名称" prop="bucketName"/>
      <el-table-column align="center" label="前缀" prop="prefix"/>
      <el-table-column align="center" label="域" prop="region"/>
      <el-table-column align="center" label="桶权限类型" prop="accessPolicy">
        <template v-slot:default="scope">
          <el-tag v-if="scope.row.accessPolicy === '0'" type="warning">private</el-tag>
          <el-tag v-if="scope.row.accessPolicy === '1'" type="success">public</el-tag>
          <el-tag v-if="scope.row.accessPolicy === '2'" type="info">custom</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否默认" prop="status">
        <template v-slot:default="scope">
          <el-switch
            v-model="scope.row.defaulted"
            active-value="0"
            inactive-value="1"
          ></el-switch>
          <!--            @change="handleStatusChange(scope.row)"-->
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template v-slot:default="scope">
          <el-button v-has-permi="['system:oss:edit']" icon="el-icon-edit" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-has-permi="['system:oss:remove']" icon="el-icon-delete" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination slot="page" v-show="pageParam.total > 0" :total="pageParam.total" :limit.sync="pageParam.pageSize"
                :page.sync="pageParam.pageNum" @pagination="getList"/>
    <oss-config-model ref="model" @ok="getList"/>
  </list-card>
</template>

<script>

import OssConfigModel from "./components/ModelCard.vue";
import {list, remove} from "../../../api/base";

export default {
  name: "OssConfig",
  components: {OssConfigModel},
  dicts: [],
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      baseUrl: '/oss/config',
      dataList: [],
      pageParam: {
        // 总条数
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {},
      // 显示搜索条件
      showSearch: false,
    };
  },
  created() {
    this.resetQuery();
  },
  methods: {
    /** 查询对象存储配置列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(res => {
        this.dataList = res.rows;
        this.pageParam.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.model.add()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.model.edit(row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选择的对象存储配置?').then(() => {
        this.loading = true;
        remove(this.baseUrl, ids).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).finally(() => {
          this.loading = false;
        });
      });
    },
    // // 对象存储配置状态修改
    // handleStatusChange(row) {
    //   let text = row.status === "0" ? "启用" : "停用";
    //   this.$modal.confirm('确认要"' + text + '""' + row.configKey + '"配置吗?').then(() => {
    //     return changeOssConfigStatus(row.ossConfigId, row.status, row.configKey);
    //   }).then(() => {
    //     this.getList()
    //     this.$modal.msgSuccess(text + "成功");
    //   }).catch(() => {
    //     row.status = row.status === "0" ? "1" : "0";
    //   })
    // }
  }
};
</script>
